﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using DAL.EstRec;
using ICSharpCode.SharpZipLib.Zip;

namespace BAL.EstRec
{
    public class PublicarTXT
    {
        public bool HasHeader { get; set; }
        public string[] Columns  {get; set;}
        public char SeparatorCharacter { get; set; }
        public string RutaGuardar { get; set; }
        public string RutaCopiar { get; set; }
        public string UnidVersion { get; set; }

        private string CopyParam1;
        private string CopyParam2;

        private List<string[]> ListaCopiar;

        public PublicarTXT(bool HasHeader, string[] Columns, char SeparatorCharacter, string RutaGuardar, string RutaCopiar, string UnidVersion)
        {
            this.HasHeader = HasHeader;
            this.SeparatorCharacter = SeparatorCharacter;
            this.RutaCopiar = RutaCopiar;
            this.RutaGuardar = RutaGuardar;
            this.Columns = Columns;
            this.UnidVersion = UnidVersion;

            //this.CopyParam1 = RutaCopiar;
            //this.CopyParam2 = RutaGuardar;
        }

        public bool exportHQ()
        {
            try
            {
                ListaCopiar = new List<string[]>();
                string query;
                string Archivoactual;
                int numTerritorios;

                query = "";
                Archivoactual = "";
                numTerritorios = 0;

                query += "SELECT DISTINCT TER.ID_TERRITORIO FROM dbo.HQBGT AS H";
                query += " INNER JOIN dbo.HQBGT_VERSION AS H_ver";
                query += " ON H_ver.UNID_HQBGT_VERSION = H.UNID_HQBGT_VERSION";
                query += " INNER JOIN dbo.R_TERRITORIO_POS AS R_POS";
                query += " ON R_POS.ID_POS = H.ID_POS";
                query += " INNER JOIN dbo.TERRITORIO AS TER";
                query += " ON TER.ID_TERRITORIO = R_POS.ID_TERRITORIO";
                query += " ORDER BY TER.ID_TERRITORIO";
                //query = "SP_ER_GET_TERRITORIOSinHQBGT";

                DataTable res = SrvDB.ExecuteQuery(query);

                numTerritorios = res.Rows.Count;

                for (int i = 0; i < numTerritorios; i++)
                {

                    DataRow dr = res.Rows[i];

                    Archivoactual = (string)dr.ItemArray[0];
                    string Territorio = Archivoactual;
                    Archivoactual += "_" + DateTime.Now.Year.ToString();

                    string aux = DateTime.Now.Month.ToString();
                    if (aux.Length == 1)
                        aux = "0" + DateTime.Now.Month.ToString();

                    Archivoactual += aux;

                    aux = DateTime.Now.Day.ToString();
                    if (aux.Length == 1)
                        aux = "0" + DateTime.Now.Day.ToString();

                    Archivoactual += aux;

                    aux = DateTime.Now.Hour.ToString();
                    if (aux.Length == 1)
                        aux = "0" + DateTime.Now.Hour.ToString();

                    Archivoactual += aux;

                    aux = DateTime.Now.Minute.ToString();
                    if (aux.Length == 1)
                        aux = "0" + DateTime.Now.Minute.ToString();

                    Archivoactual += aux;

                    aux = DateTime.Now.Second.ToString();
                    if (aux.Length == 1)
                        aux = "0" + DateTime.Now.Second.ToString();

                    Archivoactual += aux;

                    aux = DateTime.Now.Millisecond.ToString();
                    if (aux.Length == 1)
                        aux = "00" + DateTime.Now.Millisecond.ToString();
                    else if (aux.Length == 2)
                        aux = "0" + DateTime.Now.Millisecond.ToString();

                    Archivoactual += aux;

                    string queryInterno;
                    queryInterno = "EXEC SP_EXPORT_HQ @TERRITORIO = '" + Territorio + "', @VERSION = " + UnidVersion;

                    DataTable resInterno = SrvDB.ExecuteQuery(queryInterno);

                    if (resInterno.Rows.Count == 0)
                        continue;

                    //Creamos el archivo txt
                    StreamWriter sw = new StreamWriter(RutaGuardar + Archivoactual + ".txt", false, System.Text.Encoding.Default);

                    try
                    {
                        int col = 0;
                        string value = "";

                        if (HasHeader)
                        {

                            foreach (DataColumn dc in resInterno.Columns)
                            {

                                value += dc.ColumnName + SeparatorCharacter;
                            }

                            sw.WriteLine(value.Remove(value.Length - 1, 1));
                            value = "";
                        }

                        foreach (DataRow drr in resInterno.Rows)
                        {

                            foreach (DataColumn dc in resInterno.Columns)
                            {

                                value += drr.ItemArray[col].ToString() + SeparatorCharacter;
                                col += 1;
                            }

                            sw.WriteLine(value.Remove(value.Length - 1, 1));
                            value = "";
                            col = 0;
                        }

                        sw.Close();
                        sw.Dispose();
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }

                    string path = RutaGuardar;
                    FastZip fz = new FastZip();
                    fz.CreateZip(path + Archivoactual + ".zip", path, false, Archivoactual + ".txt");
                    File.Delete(RutaGuardar + Archivoactual + ".txt");

                    CopyParam1 = "";
                    CopyParam2 = "";                    

                    string[] SAux = new string[2];

                    SAux[0] = RutaGuardar + Archivoactual + ".zip";
                    SAux[1] = RutaCopiar + Archivoactual + ".zip";

                    ListaCopiar.Add(SAux);

                    //CopyParam1 = RutaGuardar + Archivoactual + ".zip";
                    //CopyParam2 = RutaCopiar + Archivoactual + ".zip";
                }

                return true;
            }
            catch (Exception Ex) {
                throw Ex;
            }
        }

        public bool exportHQcopiar() {

            try
            {   

                for (int i = 0; ListaCopiar.Count > i; i++)
                {
                    string[] SAux = ListaCopiar.ElementAt<string[]>(i);
                    File.Copy(SAux[0], SAux[1], true);                    
                }
                return true;
            }
            catch (Exception EX) {
                return false;
            }
        }

        public void export()
        {

            string querry;
            string Archivoactual;
            int numTerritorios;
            
            querry = "";
            Archivoactual = "";
            numTerritorios = 0;
            
            querry += "SELECT DISTINCT TER.TERRITORIO FROM dbo.HQBGT AS H";
	        querry += " INNER JOIN dbo.HQBGT_VERSION AS H_ver";
		    querry += " ON H_ver.UNID_HQBGT_VERSION = H.UNID_HQBGT_VERSION";
	        querry += " INNER JOIN dbo.R_TERRITORIO_POS AS R_POS";
		    querry += " ON R_POS.ID_POS = H.ID_POS";
	        querry += " INNER JOIN dbo.TERRITORIO AS TER";
		    querry += " ON TER.ID_TERRITORIO = R_POS.ID_TERRITORIO";
	        querry += " ORDER BY TER.TERRITORIO";


            DataTable res = SrvDB.ExecuteQuery(querry);

            numTerritorios = res.Rows.Count;

            for(int i = 0; i < numTerritorios; i++){

                DataRow dr = res.Rows[i];
                
                Archivoactual = (string)dr.ItemArray[0];
                //Archivoactual = dr[1];
            
                string querryInterno;
                querryInterno = "";

                querryInterno += "SELECT TER.TERRITORIO, H_ver.NAME, H.UNID_HQBGT_VERSION, H.UNID_HQBGT, H.ID_POS, H.ID_RECURSO, H.CANTIDAD FROM dbo.HQBGT AS H";
	            querryInterno += " INNER JOIN dbo.HQBGT_VERSION AS H_ver";
		        querryInterno += " ON H_ver.UNID_HQBGT_VERSION = H.UNID_HQBGT_VERSION";
	            querryInterno += " INNER JOIN dbo.R_TERRITORIO_POS AS R_POS";
		        querryInterno += " ON R_POS.ID_POS = H.ID_POS";
	            querryInterno += " INNER JOIN dbo.TERRITORIO AS TER";
		        querryInterno += " ON TER.ID_TERRITORIO = R_POS.ID_TERRITORIO";
                querryInterno += " WHERE TER.TERRITORIO = '" + Archivoactual + "'";
                
                try
                {
                    DataTable resInterno = SrvDB.ExecuteQuery(querryInterno);
                                
                    //Creamos el archivo txt
                    StreamWriter sw = new StreamWriter(RutaGuardar + Archivoactual + ".txt", false, System.Text.Encoding.Default);
                
                
                    int col = 0;
                    string value = "";

                    if (HasHeader)
                    {

                        foreach (DataColumn dc in resInterno.Columns)
                        {

                            value += dc.ColumnName + SeparatorCharacter;
                        }

                        sw.WriteLine(value.Remove(value.Length - 1, 1));
                        value = "";
                    }

                    foreach (DataRow drr in resInterno.Rows)
                    {

                        foreach (DataColumn dc in resInterno.Columns)
                        {

                            value += drr.ItemArray[col].ToString() + SeparatorCharacter;
                            col += 1;
                        }

                        sw.WriteLine(value.Remove(value.Length - 1, 1));
                        value = "";
                        col = 0;
                    }

                    sw.Close();
                    sw.Dispose();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    throw ex;
                }

                string path = RutaGuardar;
                FastZip fz = new FastZip();
                fz.CreateZip(path + Archivoactual + ".zip", path, false, Archivoactual + ".txt");
                File.Copy(RutaGuardar + Archivoactual + ".zip", RutaCopiar + Archivoactual + ".zip", true);
                File.Delete(RutaGuardar + Archivoactual + ".txt");
            }           
        }
    }
}
